package com.mc.lp.database.service.handler;

import java.util.HashSet;

import javax.annotation.Resource;

import com.mc.lp.database.service.IAttachmentService;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.shared.util.constants.ResponseStatus;
import com.mc.lp.shared.vo.AttachmentVO;
import com.mc.lp.shared.vo.FileVo;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.ServiceResponse;

public class FileHandler extends AbstractFormHandler<AttachmentVO, IValueObject> {
	
	@Resource
	IAttachmentService attachmentService;
	
	@Override
	public HashSet<FormRequestType> getRequestHandlingType() {
		HashSet<FormRequestType> requestType = new HashSet<FormRequestType>();
		requestType.add(FormRequestType.FILE_UPLOAD);
		requestType.add(FormRequestType.FILE_DELETE);
		requestType.add(FormRequestType.FILE_DOWNLOAD);
		return requestType;
	}

	@Override
	public IValueObject serveRequest(FormRequestType requestType, AttachmentVO object) throws Exception {

		switch(requestType){
			case FILE_DELETE:
				return deleteFileData(object);
			case FILE_DOWNLOAD :
				 return downloadFileData(object);
			case FILE_UPLOAD :
				return saveFileData(object);
		default:
			ServiceResponse<FileVo> serviceresponse = new ServiceResponse<FileVo>();
			serviceresponse.setMsg("Nothing to do");
			serviceresponse.setStatus(ResponseStatus.FAILED);
			return serviceresponse;
		}
	}

	private IValueObject saveFileData(AttachmentVO object) {
		ServiceResponse<FileVo> serviceresponse = new ServiceResponse<FileVo>();
		serviceresponse=getAttachmentService().saveAttachment(object);
		return serviceresponse;
	}
	
	private IValueObject deleteFileData(AttachmentVO object){
		ServiceResponse<FileVo> serviceResponse = getAttachmentService().deleteAttachment(object.getRecordId());
		return serviceResponse;
	}
	
	private IValueObject downloadFileData(AttachmentVO object){
		FileVo serviceResponse = getAttachmentService().downloadAttachmentWithData(object.getRecordId());
		if(serviceResponse==null){
			ServiceResponse<FileVo> serviceresponse = new ServiceResponse<FileVo>();
			serviceresponse.setMsg("Nothing to do");
			serviceresponse.setStatus(ResponseStatus.FAILED);
			return serviceresponse;
		}
		return serviceResponse;
	}
	
	public IAttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(IAttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}
	
}
